package top.lilele.adminSystem.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.lilele.adminSystem.dto.AddRoleResourceDao;
import top.lilele.adminSystem.entity.AdminRoleEntity;
import top.lilele.adminSystem.entity.AdminRoleResourceEntity;
import top.lilele.adminSystem.mapper.AdminRoleMapper;
import top.lilele.adminSystem.mapper.AdminRoleResourceMapper;
import top.lilele.adminSystem.mapper.AdminUserRoleMapper;
import top.lilele.adminSystem.utils.BaseResponse;
import top.lilele.adminSystem.utils.ResponseCodeEnum;
import top.lilele.adminSystem.utils.Utils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author lilele
 */
@Service
public class AdminRoleService {
    @Resource
    AdminRoleMapper adminRoleMapper;

    @Resource
    AdminRoleResourceMapper adminRoleResourceMapper;

    @Resource
    AdminUserRoleMapper adminUserRoleMapper;

    /**
     * 添加角色
     *
     * @param adminRoleEntity
     * @return
     */
    @SneakyThrows
    public BaseResponse add(AdminRoleEntity adminRoleEntity) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("roleName", adminRoleEntity.getRoleName());
        adminRoleMapper.insert(adminRoleEntity);
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }

    /**
     * 根据id查询用户的角色信息
     *
     * @param id
     * @return
     */
    public AdminRoleEntity queryRoleById(String id) {
        AdminRoleEntity info = adminRoleMapper.selectById(id);
        return info;
    }

    /**
     * 查询角色列表
     *
     * @return
     */
    public List<AdminRoleEntity> queryRoleList() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(Utils.getName(AdminRoleEntity::getIsAdmin), 2);
        List<AdminRoleEntity> list = adminRoleMapper.selectList(queryWrapper);
        return list;
    }

    /**
     * 根据id更新角色信息
     *
     * @param adminRoleEntity
     */
    public void updateRoleById(AdminRoleEntity adminRoleEntity) {
        LambdaUpdateWrapper<AdminRoleEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AdminRoleEntity::getId, adminRoleEntity.getId());
        wrapper.set(AdminRoleEntity::getRoleName, adminRoleEntity.getRoleName());
        wrapper.set(AdminRoleEntity::getDescription, adminRoleEntity.getDescription());
        wrapper.set(AdminRoleEntity::getUpdatedTime, new Date());
        adminRoleMapper.update(null, wrapper);
    }

    /**
     * 根据id删除角色信息 (开启事务，保证一系列删除逻辑成功)
     *
     * @param roleId
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void deleteRoleById(String roleId) {
        // 删除角色表
        adminRoleMapper.deleteById(roleId);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("roleId", roleId);
        // 删除角色资源关联表
        adminRoleResourceMapper.delete(queryWrapper);
        // 删除角色用户角色关联表
        adminUserRoleMapper.delete(queryWrapper);
    }

    /**
     * 绑定角色-资源
     *
     * @param addRoleResourceDao
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void addRoleResource(AddRoleResourceDao addRoleResourceDao) {
        AdminRoleResourceEntity adminRoleResourceEntity = new AdminRoleResourceEntity();
        String roleId = addRoleResourceDao.getId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("roleId", roleId);
        adminRoleResourceMapper.delete(queryWrapper);
        addRoleResourceDao.getList().stream().forEach(item -> {
            adminRoleResourceEntity.setRoleId(roleId);
            adminRoleResourceEntity.setResourceId(item);
            adminRoleResourceMapper.insert(adminRoleResourceEntity);
        });
    }
}
